package com.rongma.waterdrop.activity;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Process;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.rongma.waterdrop.R;
import com.rongma.waterdrop.global.BaseActivity;
import com.rongma.waterdrop.listener.DataFromBmobListener;
import com.rongma.waterdrop.manager.DownloadManager;
import com.rongma.waterdrop.manager.bmobmanager.TimerEventManager;
import com.rongma.waterdrop.manager.bmobmanager.UserManager;
import com.rongma.waterdrop.ui.TextProgressBar;
import com.rongma.waterdrop.utils.ActivityCollector;
import com.rongma.waterdrop.utils.LogUtils;

import cn.bmob.v3.Bmob;

public class SplashActivity extends BaseActivity {
    private EditText mEtPwd;
    private Button mBtnLogin;
    private String PASSWORD = "123456"; //用户密码
    private String BMOB_TOKEN = "04d92d0eab186007e73eeca9021830be"; //Bmob SDK的Token
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private FrameLayout layoutProgress;
    private TextProgressBar textPbLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, BMOB_TOKEN);
    }

    @Override
    protected void initViriables() {
        preferences = getSharedPreferences("downloadBmob",MODE_PRIVATE);
        editor = preferences.edit();
    }

    @Override
    protected void bindView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        layoutProgress = (FrameLayout) findViewById(R.id.layout_progress);
        textPbLoading = (TextProgressBar) findViewById(R.id.textpb);
        mBtnLogin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String inputPwd = mEtPwd.getText().toString().trim();
                if(TextUtils.isEmpty(inputPwd))
                {
                    showToast("请输入密码");
                }
                else if(!PASSWORD.equals(inputPwd))
                {
                    showToast("密码错误,请重新输入");
                }
                else
                {
                    showToast("登录成功!");
                    //判断是否为第一次安装
                    if(preferences.getBoolean("isFirst",true))
                    {
                        mBtnLogin.setEnabled(false);
                        mEtPwd.setEnabled(false);
                        LogUtils.show("第一次启动程序");
                        layoutProgress.setVisibility(View.VISIBLE);
                        synchronousFromNet();
                    }
                    else
                    {
                        finish();
                        toOtherActivity(MainActivity.class);
                    }
                }
            }
        });
        TimerEventManager.getManager(SplashActivity.this).setAllEventStop(TimerEventManager.getManager(SplashActivity.this).queryAllTimer());
    }

    //从网络同步数据
    private void synchronousFromNet() {
        final DownloadManager downloadManager = DownloadManager.getManager();
        downloadManager.setDownloadListener(new DataFromBmobListener() {
            @Override
            public void onUserSuccess() {
                textPbLoading.setProgress(20);
                downloadManager.downloadDefaultNewsData(SplashActivity.this);
            }

            @Override
            public void onNewsSuccess() {
                textPbLoading.setProgress(40);
                downloadManager.downloadTaskData(SplashActivity.this);
            }

            @Override
            public void onTaskSuccess() {
                textPbLoading.setProgress(60);
                downloadManager.downloadTimeData(SplashActivity.this);
            }

            @Override
            public void onTimeSuccess() {
                textPbLoading.setProgress(80);
                downloadManager.downloadTimeEventData(SplashActivity.this);
            }

            @Override
            public void onEventSuccess() {
                editor.putBoolean("isFirst", false);
                editor.commit();
                finish();
                toOtherActivity(MainActivity.class);
            }

            @Override
            public void onFail(int index) {
                showDialog();
                editor.putInt("failIndex",index);
                editor.commit();

            }
        });
        int index = preferences.getInt("failIndex",0);
        downloadAllDataFromBmob(index);
    }

    //下载数据
    private void downloadAllDataFromBmob(int index)
    {
        switch (index)
        {
            case 0:
                DownloadManager.getManager().downloadUserData(SplashActivity.this);
                break;
            case 1:
                DownloadManager.getManager().downloadDefaultNewsData(SplashActivity.this);
                break;
            case 2:
                DownloadManager.getManager().downloadTaskData(SplashActivity.this);
                break;
            case 3:
                DownloadManager.getManager().downloadTimeData(SplashActivity.this);
                break;
            case 4:
                DownloadManager.getManager().downloadTimeEventData(SplashActivity.this);
                break;
        }
    }


    //网络连接失败时弹出
    private void showDialog()
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(SplashActivity.this)
                .setTitle("网络未连接!")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("无法从网络同步数据,请检查网络后重试!");
        alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCollector.removeActivity(SplashActivity.this);
                Process.killProcess(Process.myPid());
                System.exit(0);
            }
        });
        alertDialog.create().show();
    }

    @Override
    protected void getData() {

    }


}
